import router from '@/router/index.ts';

import accountManageRoute from '@/router/modules/accountManageRoute.ts';

import { menuMap } from '@/data/menuItem.ts';

const whiteList: string[] = ['/login'];

router.beforeEach((to: IObj, from: IObj, next: (path?: string) => void) => {
  const userInfo = window.sessionStorage.getItem('userInfo');
  
  if (whiteList.includes(to.path)) {
    next();
  } else if (userInfo) {
    const user = JSON.parse(userInfo);

    if (user.role === '指导老师') {
      if (router.hasRoute('OrganizationManage')) {
        router.removeRoute('OrganizationManage');
      }
      Reflect.deleteProperty(menuMap, 'OrganizationManage');

      if (router.hasRoute('AccountManage')) {
        router.removeRoute('AccountManage');
      }
      Reflect.deleteProperty(menuMap, 'AccountManage');
    }
 
    next()
  } else {
    // next('/login');
    next('/login');
  }
})